﻿

-- =============================================
-- Author:		zengfei	
-- Create date: 03m/17/2012
-- Description:	更新发票明细表
-- =============================================
CREATE PROCEDURE [dbo].[Usp_ModifyInvoiceDetail]
	@p_Action	char(1),
	@p_InvoiceNo	varchar(30),
	@p_LineNo	varchar(3),
	@p_ProductNo	varchar(10),
	@p_Qty	numeric(10,2),
	@p_Price	numeric(10,2),
	@p_Amount	numeric(15,2),
	@p_TaxRate	numeric(10,4),
	@p_TaxAmount	numeric(15,2),
	@p_Remark	varchar(60),
	@p_RecordID	int out,
	@p_UserID	varchar(60),
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
begin transaction

if	@p_Action = 'A'
begin
	insert into InvoiceDetail (InvoiceNo, [LineNo], ProductNo, Qty, Price, Amount, TaxRate, TaxAmount, Remark)
		values (@p_InvoiceNo, @p_LineNo, @p_ProductNo, @p_Qty, @p_Price, @p_Amount, @p_TaxRate, @p_TaxAmount, @p_Remark)
		
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY
end
else if	@p_Action = 'C'
begin
	update InvoiceDetail set [LineNo] = @p_LineNo,
								ProductNo = @p_ProductNo,
								Qty = @p_Qty, 
								Price = @p_Price, 
								Amount = @p_Amount, 
								TaxRate = @p_TaxRate, 
								TaxAmount = @p_TaxAmount, 
								Remark = @p_Remark
							where RecordID = @p_RecordID 
							
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	delete from InvoiceDetail where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

insert into InvoiceDetail_log (InvoiceNo, [LineNo], ProductNo, Qty, Price, Amount, TaxRate, TaxAmount, Remark, RecordID, UserID, ModifyTime, [Action])
	values (@p_InvoiceNo, @p_LineNo, @p_ProductNo, @p_Qty, @p_Price, @p_Amount, @p_TaxRate, @p_TaxAmount, @p_Remark, @p_RecordID, @p_UserID, GETDATE(), @p_Action)
	
if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle

commit transaction

set @p_RFlag = 1


ErrHandle:

if	@@TRANCOUNT > 0	rollback transaction

set @p_RFlag = -1



